package com.libfintech.verification.service.impl;

import com.libfintech.verification.enumeration.ErrorEnum;
import com.libfintech.verification.service.BaseSmsService;
import com.libfintech.verification.service.Result;

import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;


public class DaYuSmsService extends BaseSmsService<DaYuConfig> {
    private static final Logger LOGGER = LoggerFactory.getLogger(DaYuSmsService.class);
    private final TaobaoClient CLIENT;

    public DaYuSmsService (DaYuConfig config) {
        super(config);
        //初始化client,使用单例方式
        CLIENT = new DefaultTaobaoClient(config.getServerUrl(),config.getAppKey(),config.getAppSecret());
    }



    /**
     * 发送短信通知
     *
     * @param smsParams 发送短信使用的参数
     * @param flag      业务标志
     * @return 返回值中的data为null或验证码图片base64
     */
    @Override
    public Result<String> sendNotification(Map<String, Object> smsParams, String flag) {
        AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();

        req.setExtend((String) smsParams.get("extend"));
        //必须填写normal
        req.setSmsType((String) smsParams.get("smsType"));
        //你应用的名字
        req.setSmsFreeSignName((String) smsParams.get("smsFreeSignName"));
        //电话号码
        req.setRecNum((String) smsParams.get("recNum"));
        //模板
        req.setSmsTemplateCode((String) smsParams.get("smsTemplateCode"));
        //模板中的参数，按照实际情况去
        req.setSmsParamString((String) smsParams.get("smsParamString"));
        try {
            AlibabaAliqinFcSmsNumSendResponse rsp = CLIENT.execute(req);
            if (rsp.getResult().getSuccess()) {
                return Result.success();
            } else {
                return Result.error(ErrorEnum.UNKNOWN);
            }
        } catch (ApiException e) {
            return Result.error(ErrorEnum.UNKNOWN);
        }
    }

    /**
     * 发送通知
     *
     * @param phoneNo 手机号码
     * @param text    通知
     */
    @Override
    public void send(String phoneNo, String text) {

    }

    /**
     * 发送短信内容
     * <p>
     * by wangxinxx
     *
     * @param smsParams 服务端确定的接口参数，采用 Map 形式。
     * @return
     */
    @Override
    public Result<Object> send(Map<String, Object> smsParams) {
        AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();

        req.setExtend((String) smsParams.get("extend"));
        //必须填写normal
        req.setSmsType((String) smsParams.get("smsType"));
        //你应用的名字
        req.setSmsFreeSignName((String) smsParams.get("smsFreeSignName"));
        //电话号码
        req.setRecNum((String) smsParams.get("recNum"));
        //模板
        req.setSmsTemplateCode((String) smsParams.get("smsTemplateCode"));
        //模板中的参数，按照实际情况去
        req.setSmsParamString((String) smsParams.get("smsParamString"));
        try {
            AlibabaAliqinFcSmsNumSendResponse rsp = CLIENT.execute(req);
            if (rsp.getResult()!=null && rsp.getResult().getSuccess()) {
                return Result.success();
            } else {
                return Result.error(ErrorEnum.UNKNOWN);
            }
        } catch (ApiException e) {
            return Result.error(ErrorEnum.UNKNOWN);
        }
    }
}
